from heapq import heapify, heappop, heappush
from typing import *


class Solution:
    def assignTasks(self, servers: List[int], tasks: List[int]) -> List[int]:
        sheap = [(servers[i], i) for i in range(len(servers))]
        heapify(sheap)
        wsheap = []
        tp = 0

        end = len(tasks)
        out = []
        finished = 0
        time = 0

        while tp < end:
            while len(wsheap) > 0 and wsheap[0][0] <= time:
                top = heappop(wsheap)
                heappush(sheap, (top[1], top[2]))

            while len(sheap) > 0 and tp <= time and tp < end:
                s = heappop(sheap)
                ws = (time + tasks[tp], s[0], s[1])
                heappush(wsheap, ws)
                out.append(s[1])
                tp += 1
            time += 1

        return out


sl = Solution()
servers = [3, 3, 2]
tasks = [1, 2, 3, 2, 1, 2]
servers = [5, 1, 4, 3, 2]
tasks = [2, 1, 2, 4, 5, 2, 1]
servers = [
    908, 103, 17, 374, 839, 318, 208, 50, 697, 393, 315, 799, 208, 675, 499,
    120, 625, 997, 992, 115, 189, 324, 150, 331, 31, 284, 587, 593, 570, 512,
    45, 794, 452, 185, 954, 474, 872, 534, 16, 189, 331, 844, 756, 661, 72,
    455, 882, 2, 927, 918, 289, 19, 766, 732, 887, 368, 998, 100, 51, 653, 372,
    284, 97, 396, 137, 535, 197, 549, 132, 640, 932, 636, 306, 134, 578, 66,
    789, 493, 931, 337, 626, 118, 623, 762, 42, 305, 997, 592, 34, 297, 812,
    698, 62, 416, 522, 713, 352, 139, 644, 877, 190, 151, 165, 290, 757, 697,
    224, 191, 906, 390, 158, 497, 422, 491, 100, 629, 782, 901, 567, 88, 571,
    145, 308, 412, 829, 805, 178, 786, 754, 405, 663, 156, 409, 505, 469, 335,
    664, 497, 986, 134, 562, 80, 228, 768, 577, 918, 722, 996, 12, 663, 578,
    729, 577, 299, 442, 632, 767, 614, 376, 364, 900, 595, 574, 772, 941, 147,
    208, 782, 90, 640, 344, 212, 152, 698, 869, 855, 946, 954, 28, 138, 451,
    698, 803, 961, 77, 735, 340, 316, 899, 333, 653, 177, 232, 914, 88, 456,
    981, 612, 777, 569, 481, 191, 802, 779, 389, 356, 75, 286, 389, 464, 975,
    353, 997, 104, 103, 39, 158, 815, 294, 451, 65, 517, 563, 724, 838, 854,
    919, 392, 945, 798, 781, 110, 678, 513, 51, 462, 26, 311, 764, 517, 330,
    752, 908, 316, 493, 830, 826, 127, 915, 969, 65, 496, 256, 490, 951, 132,
    60, 993, 126, 514, 773, 838, 23, 828, 805, 80, 783, 629, 81, 422, 271, 345,
    533, 351, 497, 393, 709, 812, 968, 557, 501, 389, 791, 710, 576, 760, 593,
    238, 683, 40, 198, 961, 380, 831, 956, 753, 936, 92, 39, 247, 692, 399,
    935, 652, 929, 14, 660, 137, 961, 364, 308, 454, 132, 264, 412, 887, 581,
    119, 660, 178, 182, 568, 405, 982, 864, 459, 632, 179, 460, 72, 814, 672,
    13, 2, 160, 584, 147, 854, 517, 8, 856, 348, 488, 810, 5, 602, 505, 316,
    130, 156, 429, 185, 686, 650, 912, 965, 795, 171, 156, 119, 688, 762, 563,
    601, 698, 774, 833, 824, 315, 463, 690, 193, 452, 616, 793, 986, 35, 30,
    393, 754, 171, 136, 100, 942, 98, 179, 596, 463, 733, 72, 117, 984, 858,
    398, 693, 372, 214, 678, 444, 409, 3, 512, 125, 688, 597, 733, 879, 58,
    643, 920, 359, 862, 536, 28, 815, 410, 537, 31, 539, 333, 187, 896, 305,
    483, 364, 772, 127, 462, 769, 671, 978, 86, 930
]
tasks = [
    567, 66, 79, 965, 224, 834, 143, 80, 21, 846, 986, 565, 610, 962, 946, 829,
    127, 319, 436, 648, 732, 117, 125, 504, 80, 725, 801, 323, 328, 414, 557,
    739, 958, 828, 509, 695, 502, 555, 101, 706, 154, 35, 479, 587, 811, 799,
    804, 959, 728, 439, 644, 542, 106, 54, 159, 42, 956, 866, 490, 251, 237,
    191, 313, 705, 490, 11, 572, 476, 118, 787, 185, 101, 551, 788, 536, 558,
    594, 867, 114, 313, 154, 758, 579, 235, 655, 387, 339, 398, 570, 166, 660,
    361, 200, 737, 513, 675, 59, 148, 920, 774, 746, 105, 947, 267, 646, 196,
    472, 174, 744, 891, 796, 269, 59, 470, 385, 936, 317, 893, 13, 130, 188,
    918, 14, 862, 567, 714, 654, 843, 570, 417, 109, 585, 259, 715, 395, 850,
    326, 913, 543, 254, 713, 377, 483, 469, 947, 772, 67, 100, 352, 187, 35,
    725, 855, 664, 771, 158, 52, 441, 343, 432, 732, 398, 772, 1000, 691, 751,
    481, 426, 419, 930, 930, 379, 995, 847, 627, 991, 490, 907, 312, 641, 88,
    113, 141, 389, 522, 922, 444, 431, 457, 510, 995, 68, 943, 562, 756, 705,
    428, 628, 574, 24, 442, 344, 321, 45, 654, 206, 493, 375, 928, 166, 955,
    762, 103, 1000, 265, 365, 25, 990, 412, 73, 839, 737, 563, 399, 673, 423,
    409, 27, 200, 203, 976, 358, 244, 350, 897, 92, 196, 542, 599, 272, 374,
    598, 892, 887, 549, 687, 551, 904, 50, 334, 448, 603, 207, 119, 689, 346,
    426, 781, 761, 580, 822, 347, 166, 521, 959, 905, 574, 966, 699, 362, 23,
    745, 392, 359, 736, 137, 969, 277, 842, 834, 177, 963, 606, 811, 871, 702,
    801, 389, 485, 923, 459, 846, 32, 609, 747, 981, 607, 767, 831, 240, 904,
    702, 334, 534, 74, 603, 228, 684, 718, 427, 567, 457, 690, 563, 359, 233,
    131, 703, 20, 342, 74, 863, 141, 808, 428, 83, 622, 552, 214, 9, 698, 445,
    214, 399, 140, 648, 121, 257, 692, 304, 713, 541, 790, 310, 604, 449, 948,
    248, 905, 680, 441, 863, 132, 528, 108, 576, 109, 318, 50, 469, 340, 139,
    971, 194, 528, 808, 107, 396, 717, 511, 627, 246, 920, 469, 959, 579, 932,
    336, 478, 164, 566, 17, 852, 131, 160, 387, 66, 917, 376, 593, 31, 934,
    598, 283, 448, 998, 293, 513, 252, 212, 725, 868, 246, 720, 922, 259, 180,
    502, 698, 438, 463, 728, 810, 219, 487, 54, 106, 816, 924, 287, 201, 184,
    757, 897, 601, 258, 620, 124, 701, 777, 688, 28, 568, 561, 766, 900, 432,
    476, 244, 101, 256, 549, 375, 246, 265, 898, 13, 764, 399, 428, 434, 603,
    269, 69, 710, 674, 759, 42, 374, 450, 926, 720, 235, 595, 297, 606, 362,
    666, 313, 745, 571, 57, 366, 242, 757, 512, 287, 908, 316, 323, 238, 454,
    955, 980, 889, 490, 67, 515, 672, 134, 757, 466, 563, 243, 944, 130, 315,
    722, 790, 157, 474, 941, 950, 957, 783, 31, 831, 353, 763, 250, 823, 782,
    766, 844, 637, 16, 811, 907, 363, 233, 60, 425, 814, 159, 508, 37, 805,
    739, 743, 12, 249, 461, 143, 571, 39, 833, 467, 393, 232, 416, 81, 136,
    831, 450, 937, 322, 262, 353, 931, 85, 701, 995, 215, 360, 675, 335, 886,
    579, 745, 64, 215, 798, 851, 802, 401, 500, 151, 293, 163, 17, 377, 304,
    640, 663, 217, 771, 546, 471, 430, 809, 747, 983, 150, 525, 639, 225, 325,
    24, 471, 195, 216, 601, 754, 330, 357, 379, 961, 309, 811, 156, 650, 905,
    806, 19, 986, 979, 734, 112, 942, 552, 530, 255, 912, 699, 35, 738, 779,
    557, 492, 998, 85, 698, 267, 407, 206, 674, 960, 882, 610, 535, 434, 524,
    500, 195, 198, 549, 286, 807, 169, 86, 441, 253, 954, 70, 703, 616, 623,
    355, 536, 598, 857, 169, 526, 815, 307, 45, 732, 365, 457, 174, 80, 770,
    483, 390, 958, 960, 208, 675, 536, 803, 635, 21, 319, 502, 835, 344, 503,
    270, 258, 285, 419, 501, 749, 476, 223, 356, 736, 730, 848, 712, 55, 146,
    447, 627, 438, 119, 588, 96, 13, 141, 376, 453, 324, 967, 966, 100
]
print(sl.assignTasks(servers, tasks))
